package com.wn.class30.cotroller.shoppingmall;

import com.wn.class30.entity.Product;
import com.wn.class30.entity.Shoppingcar;
import com.wn.class30.service.ShoppingCarService;
import com.wn.class30.tools.Result;
import com.wn.class30.tools.UseridUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ShoppingMallCar")
@Slf4j
@Api("商城购物车")
public class ShoppingCarController {
    @Resource
    private UseridUtils useridUtils;
    @Resource
    private ShoppingCarService shoppingCarService;

    @GetMapping("/getOneProduct")
    @ApiOperation(value = "商城购物车查询单个商品方法")
    public Shoppingcar getOneProduct(Shoppingcar shoppingcar) throws Exception {
        return  shoppingCarService.getOneProduct(shoppingcar);
    }

    //商城购物车添加商品方法
    @GetMapping("/insertProduct")
    @ApiOperation(value = "商城购物车添加商品方法")
    public Result insertProduct(HttpServletRequest req, @Valid @NotNull Shoppingcar shoppingcar) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        System.out.println("userid="+userid);
        if(userid == null){
            System.out.println("游客，没得userid，跳转登录");
            return  new Result("200","success",null,null);
        }else{
            shoppingcar.setUserId(userid);
            shoppingcar.setProductNum(1);
            System.out.println("shoppingcar"+shoppingcar);
            //判断购物车是否已经存在改产品：根据用户id和商品id判断
            Shoppingcar oneProduct = this.getOneProduct(shoppingcar);
            if (oneProduct != null){
                //已经存在，不操作
            }else{
                //添加商品到购物车
                Integer resultInt = shoppingCarService.insertProduct(shoppingcar);
            }
            return  new Result("200","success",null,null);
        }
    }

    //根据用户查询购物车中的店铺
    @GetMapping("/getBusinessByUserId")
    @ApiOperation(value = "根据用户查询购物车中的店铺")
    public Result getBusinessByUserId(HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        System.out.println("userid="+userid);
        if(userid == null){
            System.out.println("游客，没得userid，跳转登录");
            return  new Result("200","success",null,null);
        }else{
            List<Map<String, Object>> businessByUserId = shoppingCarService.getBusinessByUserId(userid);
            return  new Result("200","success",null,businessByUserId);
        }
    }
    //根据用户id查询购物车中的商品详情 ，仅查询了有用的字段
    @GetMapping("/getProductByUserId")
    @ApiOperation(value = "根据用户id查询购物车中的商品详情")
    public Result getProductByUserId(HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        System.out.println("userid="+userid);
        if(userid == null){
            System.out.println("游客，没得userid，跳转登录");
            return  new Result("200","success",null,null);
        }else{
            List<Map<String, Object>> productByUserId = shoppingCarService.getProductByUserId(userid);
            return  new Result("200","success",null,productByUserId);
        }
    }
    //根据加减修改数据库中商品数量
    @GetMapping("/updateProductNum")
    @ApiOperation(value = "根据加减修改数据库中商品数量")
    public Result updateProductNum(HttpServletRequest req,@Valid @NotNull Shoppingcar shoppingcar) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        System.out.println("userid="+userid);
        if(userid == null){
            System.out.println("游客，没得userid，跳转登录");
            return  new Result("200","success",null,null);
        }else{
            shoppingCarService.updateProductNum(shoppingcar);
            return  new Result("200","success",null,null);
        }
    }
    //删除购物车中的商品
    @GetMapping("/deleteCarProduct")
    @ApiOperation(value = "删除购物车中的商品")
    public Result deleteCarProduct(HttpServletRequest req,@Valid @NotNull Integer shoppingcarId) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        System.out.println("userid="+userid);
        if(userid == null){
            System.out.println("游客，没得userid，跳转登录");
            return  new Result("200","success",null,null);
        }else{
            shoppingCarService.deleteCarProduct(shoppingcarId);
            return  new Result("200","success",null,null);
        }
    }


}
